package org.anderground.editor.console;

import java.io.File;
import java.io.FileNotFoundException;

import org.anderground.core.Logger;
import org.anderground.core.model.ModelData;
import org.anderground.editor.model.EditorModel;
import org.anderground.editor.storage.ReadersRegistry;
import org.anderground.editor.storage.ReadersRegistry.FileFormat;
import org.anderground.editor.tools.ToolsRegistry;
import org.anderground.modules.editor.importers.ModelImporter;
import org.anderground.modules.editor.tools.EditorTool;
import org.anderground.modules.storage.xml.SaxModelReader;

public class EditorConsole {
	
	public static void processArgs(String[] args) {
		ArgMatcher matcher = new ArgMatcher(args);
		for(Arg arg: matcher.getArgs()) {
			Logger.i("arg: "+arg);
		}
		for(String arg: matcher.getFiles()) {
			Logger.i("file to open: " + arg);
		}
		//Logger.i(matcher.getFolders())
		if (matcher.getFiles().length > 1
				|| (matcher.getFiles().length > 0 && (matcher.haveArg("c") || matcher
						.haveArg("console"))))
			for (String str : matcher.getFiles()) {
				File file = new File(str);
				openFile(file);
				String folder = file.getAbsolutePath().substring(0,file.getAbsolutePath().lastIndexOf(file.separatorChar));
				String fileName = file.getName().substring(0,
						file.getName().lastIndexOf('.'));
				if (matcher.getFolders().length > 0)
					folder = matcher.getFolders()[0];
				EditorModel.getInstance().save(folder +File.separatorChar+ fileName + ".xml");
				
			}
		else if(matcher.getFiles().length==1){
			File file = new File(matcher.getFiles()[0]);
			openFile(file);
		}
		for(Class<? extends EditorTool> toolClass: ToolsRegistry.getTools()) {
			try {
				EditorTool tool = toolClass.newInstance();
				for(String arg: tool.getConsoleArgs()) {
					if(matcher.haveArg(arg)) {
						Arg a = matcher.getArg(arg);
						ModelData data = EditorModel.getInstance().getData();
						tool.callInConsole(data,a.name,a.getSubargs());
					}
				}
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//for
		}
	}
	
	private static void openFile(File file) {
		try {
			SaxModelReader reader = new SaxModelReader(file);
			EditorModel.getInstance().load(reader.read());
			System.out.println("loaded by standard reader");
		} catch (Exception e) {
			System.out.println("non standart reader: try to find importer");
			ModelImporter importer = getReader(file);
			if (importer != null) {
				try {
					System.out.println("importer have been found :"
							+ importer.getClass().getName());
					EditorModel.getInstance().load(importer.read(file));
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					 e1.printStackTrace();
				}
			} else {
				System.out.println("can't open file: " + file.getAbsolutePath()
						+ "(unknown format)");
			}
		}
	}
	private static ModelImporter getReader(File file) {
		for(FileFormat format: ReadersRegistry.getFormats()) {
			try {
				ModelImporter importer = format.getImporterClass().newInstance();
				if(importer.canReadFile(file)) return importer;
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
}
